package com.zjuee.core.translation.factory;

import com.zjuee.core.translation.handledata.DataHandlerStrategy;
import com.zjuee.core.translation.utils.ReflectionUtil;
import com.zjuee.core.translation.utils.SpringContextUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 策略工厂
 */
public class DataHandleStrategyFactory {

    private  Map<String, DataHandlerStrategy> dataHandlerStrategyMap;

    public DataHandlerStrategy getDataHandleStrategy(String dataType){
        return dataHandlerStrategyMap.get(dataType);
    }
    //通过反射获取实例
    public DataHandleStrategyFactory(){
        this.dataHandlerStrategyMap = new HashMap<>();
        //获取IReceiptHandler接口的实现类
        Set<Class<?>> classList = ReflectionUtil.getClassSetBySuper(DataHandlerStrategy.class);
        if (classList != null && classList.size() > 0) {
            for (Class<?> clazz : classList) {
                try {
                    if(ObjectUtils.isNotEmpty(clazz.getAnnotation(Component.class))){
                        this.dataHandlerStrategyMap.put(clazz.getAnnotation(Component.class).value(), (DataHandlerStrategy) SpringContextUtil.getBean(clazz.getAnnotation(Component.class).value()));
                    }
                } catch ( Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
